package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.web.exception.BusinessException;
import com.itheima.web.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.plugin2.message.Message;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Guanxing
 * @date 2021/9/30
 */
@RestController
public class AdminController {

    @Autowired
    private AdminManager adminManager;

    //生成验证码
    @GetMapping("/system/users/verification")
    public void createCodeImg(String uuid, HttpServletResponse response) throws IOException {
        //生成验证码
        LineCaptcha lineCaptcha = adminManager.createCode(uuid);
        //输出给浏览器
        lineCaptcha.write(response.getOutputStream());
    }

    //登录
    @PostMapping("/system/users/login")
    public Map login(@RequestBody Map<String, String> requestMap) throws BusinessException {
        String username = requestMap.get("username");
        String password = requestMap.get("password");
        String verificationCode = requestMap.get("verificationCode");
        String uuid = requestMap.get("uuid");

        String token = adminManager.login(username, password, verificationCode, uuid);

        //构建返回值
        Map map = new HashMap();
        map.put("token", token);
        return map;
    }

    //登录之后发起的查询用户请求
    @PostMapping("/system/users/profile")
    public Admin findAdminInfo(@RequestHeader("Authorization") String token) {
        return adminManager.findAminInfo(token);
    }

    //登出
    @PostMapping("/system/users/logout")
    public void logout(@RequestHeader("Authorization") String token) {
        adminManager.logout(token);
    }

    //动态复审-通过
    @PostMapping("/manage/messages/pass")
    public Message messagesPass(@RequestBody String[] ids) {
        return adminManager.messagesPass(ids);
    }

    //动态复审-驳回
    @PostMapping("/manage/messages/reject")
    public Message messagesReject(@RequestBody String[] ids) {
        return adminManager.messagesReject(ids);
    }
}
